<?php
/**
 * Created by PhpStorm.
 * User: liuxiaojie
 * Date: 2019-04-29
 * Time: 17:46
 */

namespace app\repair\controller;

use app\common\Base;
use app\model\Company;
use app\model\ServiceConfig;

class Config extends Base
{

    function service()
    {
        $data = ServiceConfig::where(['status'=> 1])->field('id as service_type, name as service, icon, description')->select();

        return success($data);
    }

    function games()
    {
        $list = db('games')->where(['status'=> 1])->field('game_id, name, desc, number, image, app_id, path, extraData')->order('sort desc')->limit(8)->select();
        $list1 = db('ad')->where(['status'=> 1, 'location'=> 1])->select();
        $list2 = db('ad')->where(['status'=> 1, 'location'=> 5])->find();
        $zaiaj = db('category')->where(['category_id'=> ['>', 1]])->select();
        return success([
            'game'=> $list,
            'ad'=> $list1,
            'ad2'=> $list2,
            'zaiaj'=> $zaiaj,
            'is_open'=> 0
        ]);
    }

    function ad()
    {
        $location = $this->getParam('location', 1);
        $list = db('ad')->where(['status'=> 1, 'location'=> $location])->select();
        return success($list);
    }

    function money()
    {
        $type = $this->getParam('type', 1);
        $uid = $this->getParam('u_id', 0);
        $pay_money = ServiceConfig::where(['id'=> $type])->field('terrace_money, commission, earnest_money, description, image, fees_desc')->find();
        $total_money = $pay_money['terrace_money'] + $pay_money['commission'] + $pay_money['earnest_money'];

        $description = explode(',', $pay_money['description']);
        $money = db('customer')->where(['id'=> $uid])->value('money_xn');
        if($money>=$total_money)
        {
            $is_ok = 1;
        }else{
            $is_ok = 0;
        }

        return success(['money'=> $total_money, 'description'=> $description, 'is_ok'=> $is_ok, 'image'=> $pay_money['image'], 'fees_desc'=> $pay_money['fees_desc']]);
    }

    function businessList()
    {
        $data = [
            'category_id'=> $this->getParam('category_id', 0),
        ];

        $keyword = $this->getParam('keyword', '');
        if($keyword) $data['name'] = ['like', "%$keyword%"];

        $data['status'] = 3;

        $lat = $this->getParam('lat', 0);
        $lng = $this->getParam('lng', 0);

        $list = Company::where($data)->field("id, name, tag, cover, address, (st_distance (point (lng, lat),point($lng,$lat) ) * 111195) AS distance")
            ->order('distance asc')
            ->paginate($this->pageSize)->each(function (&$item){
                $item['tag'] = explode(',', $item['tag']);
                if($item['distance']/1000>1)
                {
                    $item['distance'] = round($item['distance']/1000).'km';
                }else{
                    $item['distance'] = intval($item['distance']).'m';
                }
                return $item;
            });
        return success($list);
    }

    function businessDetail()
    {
        $id = $this->getParam('id', 0);

        $data = Company::where([
            'id'=> $id
        ])->field('id, name, tag, tel as mobile, address, cover, ad_img, lat, lng, qrcode, images')->find();

        $data['tag'] = explode(',', $data['tag']);
        if($data['images'])
        {
            $data['images'] = explode(',', $data['images']);
        }else{
            $data['images'] = [];
        }

        return success($data);
    }
}